using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using GestaoEventos.Interface;

namespace GestaoEventos.DataAccess
{
      public class D_Evento
      {
          #region Atributos

          private SqlConnection con;

          #endregion

          #region Construtores

          public D_Evento()
          {
              con = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"].ToString());
          }

          #endregion

          #region Métodos Publicos

          public int Insert(I_Evento IEvento)
          {
              try
              {
                  string sql = "SPI_Evento";
                  SqlCommand cmd = new SqlCommand(sql);

                  cmd.Parameters.Add(new SqlParameter("@pIdCampus", IEvento.IdCampus));
                  cmd.Parameters.Add(new SqlParameter("@pIdUsuario", IEvento.IdUsuario));
                  cmd.Parameters.Add(new SqlParameter("@pIdStatusEvento", IEvento.IdStatusEvento));
                  cmd.Parameters.Add(new SqlParameter("@pNome", IEvento.Nome));
                  if (IEvento.DataCriacao >= Convert.ToDateTime("1/1/1753"))
                      cmd.Parameters.Add(new SqlParameter("@pDataCriacao", IEvento.DataCriacao));
                  if (IEvento.DataEvento >= Convert.ToDateTime("1/1/1753"))
                      cmd.Parameters.Add(new SqlParameter("@pDataEvento", IEvento.DataEvento));
                  cmd.Parameters.Add(new SqlParameter("@pHoraInicio", IEvento.HoraInicio));
                  cmd.Parameters.Add(new SqlParameter("@pHoraFim", IEvento.HoraFim));
                  cmd.Parameters.Add(new SqlParameter("@pResponsavel", IEvento.Responsavel));

                  SqlParameter param = cmd.Parameters.Add("@pReturn", SqlDbType.Int);
                  param.Direction = ParameterDirection.Output;

                  con.Open();
                  cmd.Connection = con;
                  cmd.CommandType = CommandType.StoredProcedure;
                  cmd.ExecuteNonQuery();
                  con.Close();

                  return Convert.ToInt32(param.Value);
              }
              catch(Exception e)
              {
                  throw e;
              }
          }
          public int Update(I_Evento IEvento)
          {
              try
              {
                  string sql = "SPU_Evento";
                  SqlCommand cmd = new SqlCommand(sql);

                  cmd.Parameters.Add(new SqlParameter("@pId", IEvento.Id));
                  cmd.Parameters.Add(new SqlParameter("@pIdCampus", IEvento.IdCampus));
                  cmd.Parameters.Add(new SqlParameter("@pIdUsuario", IEvento.IdUsuario));
                  cmd.Parameters.Add(new SqlParameter("@pIdStatusEvento", IEvento.IdStatusEvento));
                  cmd.Parameters.Add(new SqlParameter("@pNome", IEvento.Nome));
                  if (IEvento.DataCriacao >= Convert.ToDateTime("1/1/1753"))
                      cmd.Parameters.Add(new SqlParameter("@pDataCriacao", IEvento.DataCriacao));
                  if (IEvento.DataEvento >= Convert.ToDateTime("1/1/1753"))
                      cmd.Parameters.Add(new SqlParameter("@pDataEvento", IEvento.DataEvento));
                  cmd.Parameters.Add(new SqlParameter("@pHoraInicio", IEvento.HoraInicio));
                  cmd.Parameters.Add(new SqlParameter("@pHoraFim", IEvento.HoraFim));
                  cmd.Parameters.Add(new SqlParameter("@pResponsavel", IEvento.Responsavel));

                  con.Open();
                  cmd.Connection = con;
                  cmd.CommandType = CommandType.StoredProcedure;
                  int RecordsAffected = cmd.ExecuteNonQuery();
                  con.Close();

                  return RecordsAffected;
              }
              catch(Exception e)
              {
                  throw e;
              }
          }
          public int Delete(I_Evento IEvento)
          {
              try
              {
                  string sql = "SPD_EVENTO";
                  SqlCommand cmd = new SqlCommand(sql);

                  cmd.Parameters.Add(new SqlParameter("@pId", IEvento.Id));

                  con.Open();
                  cmd.Connection = con;
                  cmd.CommandType = CommandType.StoredProcedure;
                  int RecordsAffected = cmd.ExecuteNonQuery();
                  con.Close();

                  return RecordsAffected;
              }
              catch(Exception e)
              {
                  throw e;
              }
          }
          public ArrayList Select(I_Evento IEvento)
          {
              try
              {
                  string sql = "SPS_Evento";
                  SqlCommand cmd = new SqlCommand(sql);

                  if (IEvento.Id != 0)
                      cmd.Parameters.Add(new SqlParameter("@pId", IEvento.Id));
                  if (IEvento.IdCampus != 0)
                      cmd.Parameters.Add(new SqlParameter("@pIdCampus", IEvento.IdCampus));
                  if (IEvento.IdUsuario != 0)
                      cmd.Parameters.Add(new SqlParameter("@pIdUsuario", IEvento.IdUsuario));
                  if (IEvento.IdStatusEvento != 0)
                      cmd.Parameters.Add(new SqlParameter("@pIdStatusEvento", IEvento.IdStatusEvento));
                  cmd.Parameters.Add(new SqlParameter("@pNome", IEvento.Nome));
                  if (IEvento.DataCriacao >= Convert.ToDateTime("1/1/1753"))
                      cmd.Parameters.Add(new SqlParameter("@pDataCriacao", IEvento.DataCriacao.ToString("dd/MM/yyyy")));
                  if (IEvento.DataEvento >= Convert.ToDateTime("1/1/1753"))
                      cmd.Parameters.Add(new SqlParameter("@pDataEvento", IEvento.DataEvento.ToString("dd/MM/yyyy")));
                  cmd.Parameters.Add(new SqlParameter("@pHoraInicio", IEvento.HoraInicio));
                  cmd.Parameters.Add(new SqlParameter("@pHoraFim", IEvento.HoraFim));
                  cmd.Parameters.Add(new SqlParameter("@pResponsavel", IEvento.Responsavel));

                  con.Open();
                  cmd.Connection = con;
                  cmd.CommandType = CommandType.StoredProcedure;

                  SqlDataAdapter da = new SqlDataAdapter(cmd);
                  DataTable dt = new DataTable();
                  da.Fill(dt);

                  con.Close();

                  ArrayList al = new ArrayList();
                  I_Evento IObj;
                  foreach(DataRow dr in dt.Rows)
                  {
                      IObj = new I_Evento();
                      IObj.Id = Convert.ToInt32(dr["Id"]);
                      IObj.IdCampus = Convert.ToInt32(dr["IdCampus"]);
                      IObj.IdUsuario = Convert.ToInt32(dr["IdUsuario"]);
                      IObj.IdStatusEvento = Convert.ToInt32(dr["IdStatusEvento"]);
                      IObj.Nome = dr["Nome"].ToString();
                      IObj.DataCriacao = Convert.ToDateTime(dr["DataCriacao"]);
                      IObj.DataEvento = Convert.ToDateTime(dr["DataEvento"]);
                      IObj.HoraInicio = dr["HoraInicio"].ToString();
                      IObj.HoraFim = dr["HoraFim"].ToString();
                      IObj.Responsavel = dr["Responsavel"].ToString();
                      IObj.OutrosResponsaveis = dr["OutrosResponsaveis"].ToString();

                      al.Add(IObj);
                  }

                  return al;
              }
              catch(Exception e)
              {
                  throw e;
              }
          }

          #endregion
      }
}

